home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / pgpserveruam.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  7.7 KB  |  312 lines

  1. // 
  2. // Apple Macintosh Developer Technical Support
  3. // Written by:  Vinnie Moscaritolo
  4. //
  5. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  6. //
  7. // You may incorporate this sample code into your applications without
  8. // restriction, though the sample code has been provided "AS IS" and the
  9. // responsibility for its operation is 100% yours.  However, what you are
  10. // not permitted to do is to redistribute the source as "DSC Sample Code"
  11. // after having made changes. If you're going to re-distribute the source,
  12. // we require that you make it clear in the source that the code was
  13. // descended from Apple Sample Code, but that you've made changes.
  14. // 
  15.  
  16. #include <types.h>
  17.  
  18.  
  19. typedef UInt32 KernelID;
  20.  
  21. #include <string.h>
  22.  
  23. #include <PLStringFuncs.h>
  24. #include <CodeFragments.h> 
  25.  
  26. #include "AppleShareRegistry.h"
  27. #include "AppleShareFileServerRegistry.h"
  28.  
  29.  #include "UAM.h"
  30. //#include "OAMTypes.h"
  31.  
  32.  
  33. #define PGP_MACINTOSH 1
  34.  
  35. #include "pgpErrors.h"
  36. #include "pgpKeys.h"
  37. #include "pgpMemoryMgr.h"
  38. #include "pgpUtilities.h"
  39. #include "pgpFeatures.h"
  40. #include "pgpHash.h"
  41. #include "pgpPublicKey.h"
  42. #include "TPGPException.h"
  43. #include "TMacException.h"
  44. #include "TPGPkey.h"
  45. #include "TMemPGPkey.h"
  46. #include "PGPUAMmsgFormat.h"
  47. #include "PGPServerMemory.h"
  48. #include "ASIPChallenge.h"
  49.  
  50. // ---------------------------------------------------------------------------
  51. #pragma mark  Prototypes 
  52. // ---------------------------------------------------------------------------
  53.  
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #pragma export on
  58.  
  59.      unsigned long UAMVersion = 0;
  60.     unsigned long UAMFlags = 0;
  61.     unsigned char *UAMName = "\pPGPUAM 1.0";
  62.  
  63.             OAMStatus UAMAuthenticate (    int operation, int id,
  64.                                         void* authState, int authStateSize,
  65.                                         void* authData, int authDataSize,
  66.                                         void* authStateOut, int* authStateSizeOut, 
  67.                                         void* authDataOut, int* authDataSizeOut);
  68.  
  69.     pascal  OSErr     UAMSetUP(const CFragInitBlock *theInitBlock);
  70.     pascal    void    UAMCleanUP(void);
  71.  
  72.     pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
  73.     pascal OSErr __terminate(void);
  74.  
  75. #pragma export off
  76.  
  77.  
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. static void BuildObjectSpecByNameType(OAMObjectSpec *obj, StringPtr name, OAMType type);
  82.  
  83.  
  84. // ---------------------------------------------------------------------------
  85. #pragma mark  Globals 
  86. // ---------------------------------------------------------------------------
  87.  
  88. static    TMemPGPkey *    gServerKey             = nil;
  89. static    PGPMemoryMgrRef gMemMgr                = nil;
  90. static     Boolean            gReloadServerKey     = false;
  91.  
  92.  
  93. class TLoginState
  94. {
  95.  public: 
  96.  
  97.      void*     operator new     (size_t size)         { return ::OTAllocMem (size); };
  98.      void     operator delete (void* deadObject)     { ::OTFreeMem(deadObject); };
  99.  
  100. // PRIVATE FIELDS
  101.     TMemPGPkey    fClientKey;
  102.     Str255        fCounterChallengeString;
  103.  
  104.  };
  105.  
  106.  
  107.  
  108. // ---------------------------------------------------------------------------
  109. static void BuildObjectSpecByNameType(OAMObjectSpec *obj, StringPtr name, OAMType type)
  110. // ---------------------------------------------------------------------------
  111. //
  112. {
  113.     short len = 0;
  114.     
  115.     memset(obj, 0, sizeof(OAMObjectSpec));
  116.     obj->specType = kOAMObjectSpecByNameType;
  117.     obj->objectType = type;
  118.     len = *name + 1;
  119.     memcpy(obj->u.name, name, len);
  120. }
  121.  
  122.  
  123. // ---------------------------------------------------------------------------
  124. OAMStatus UAMAuthenticate
  125.     (int operation, int id,
  126.     void* authState, int authStateSize, void* authData, int authDataSize,
  127.     void* authStateOut, int* authStateSizeOut, void* authDataOut, int* authDataSizeOut)
  128. // ---------------------------------------------------------------------------
  129. // 
  130. {
  131.     *authStateSizeOut = 0;
  132.     *authDataSizeOut = 0;
  133.      
  134.     OAMStatus err = kOAMAuthenticationErr;
  135.  
  136.     try
  137.     {
  138.          switch (operation) 
  139.         {
  140.             case kUAMAuthLogin:                 
  141.   //    DebugStr("\pPGP UAMAuthenticate operation = kUAMAuthLogin");
  142.     
  143.                 // check if server key needs to be reloaded
  144.                 if(gReloadServerKey)    
  145.                  {
  146.                     OAMObjectSpec    obj;    
  147.                     unsigned char     keyBuf[1024];
  148.                     UInt32            bufLen = sizeof (keyBuf);
  149.                     
  150. //                    DebugStr("\pInitializing Server Key");
  151.  
  152.                     memset(&obj, 0, sizeof(OAMObjectSpec));
  153.                     obj.specType = kOAMObjectSpecByShortID;
  154.                     obj.u.shortID = kOAMMachineShortID;
  155.  
  156.                     err = UAMGetAttribute(&obj, kOAMMachine, 'PGPs', (void*) keyBuf, &bufLen);
  157.                      if(err == noErr)
  158.                          {
  159.                           gServerKey->Initialize( (void*)keyBuf, bufLen );
  160.                          gReloadServerKey = false;
  161.                          }
  162.                 }
  163.              
  164.             // Do authentication challenge.
  165.                  {
  166.                     PUAM_LOGIN_CMD     cmd;
  167.                     TLoginState*      stateP;
  168.                      
  169.                     OAMObjectSpec    obj;    
  170.                     unsigned char     keyBuf[1024];
  171.                     UInt32            bufLen = sizeof (keyBuf);
  172.                       Str32            fpString;
  173.  
  174.                      ParseLoginCmd(authData,  (unsigned long *) &authDataSize,  &cmd);
  175.  
  176.                        // find users key
  177.                       memset(&obj, 0, sizeof(OAMObjectSpec));
  178.                       BuildObjectSpecByNameType(&obj,(StringPtr) cmd.userName, kOAMUser);
  179.                      
  180.                      err = UAMGetAttribute(&obj, kOAMUser, 'PGPs', (void*) keyBuf, &bufLen);
  181.  
  182.                     if(err != noErr) break;
  183.                     
  184.                     stateP = new TLoginState();
  185.                     stateP->fClientKey.Initialize((void*) keyBuf, bufLen );
  186.                                 
  187.                       // build Challenge String .
  188.                      stateP->fClientKey.GetFingerprintBinaryPString(fpString ); 
  189.                        ReplyToChallenge(gServerKey, "\0", &stateP->fClientKey, (StringPtr) cmd.challengeString, stateP->fCounterChallengeString);
  190.                       
  191.                       *authDataSizeOut = ((char*) FormatLoginResp(authDataOut, stateP->fCounterChallengeString, fpString) - (char*)authDataOut);
  192.                 
  193.                      memcpy( authStateOut, &stateP, sizeof stateP );
  194.                     *authStateSizeOut = sizeof stateP;
  195.                     
  196.                       err = kOAMAuthenticationInProgressErr;    
  197.                  }
  198.                          
  199.                  break;
  200.                 
  201.             case kUAMAuthLoginContinue:    
  202.                 err = kOAMAuthenticationErr;
  203.                 if (authStateSize == sizeof (TLoginState*) )
  204.                 {
  205.                      PUAM_LOGIN_CONT_CMD    cmd;
  206.                     TLoginState*     stateP =  nil;
  207.                       
  208.                        memcpy( &stateP,authState, sizeof stateP );
  209.                     if(stateP) 
  210.                     {
  211.                          ParseLoginContinueCmd(authData,  (unsigned long *) &authDataSize,  &cmd);
  212.                             err =  VerifyCounterChallenge( &stateP->fClientKey,  stateP->fCounterChallengeString, cmd.SigPString) 
  213.                                         ? noErr : kOAMAuthenticationErr;
  214.                         delete stateP;
  215.                     }
  216.                  }
  217.                  
  218.                  *authStateSizeOut = 0;
  219.                   break;
  220.                 
  221.             case kUAMAuthChangeKey:            // for change password
  222.                 DebugStr("\pPGP UAMAuthenticate operation = kUAMAuthChangeKey");
  223.                 break;
  224.                 
  225.             case kUAMAuthChangeKeyContinue:    // for change password
  226.                 DebugStr("\pPGP UAMAuthenticate operation = kUAMAuthChangeKeyContinue");
  227.                 break;
  228.             
  229.         }
  230.     }
  231. // handle PGP errors
  232.     catch (TPGPException &ex)
  233.     {
  234.         DebugStr("\pTPGPException");
  235.         err = kOAMAuthenticationErr;
  236.     
  237.     }
  238.  
  239.     return err;
  240. }
  241.  
  242.  
  243.  
  244. // ---------------------------------------------------------------------------
  245. pascal OSErr UAMSetUP(const CFragInitBlock *theInitBlock)
  246. // ---------------------------------------------------------------------------
  247. // 
  248.     {
  249.      OSStatus err = noErr;
  250.      
  251. //    DebugStr("\pPGP UAMSetUP");
  252.     err = __initialize( theInitBlock );
  253.     if (err != noErr) return err;
  254.  
  255.     try
  256.     {
  257.     
  258.          // Create a custom PGP context
  259.         ThrowIfMacErr(  InitializeServerMemory(&gMemMgr));
  260.           
  261.         TPGPkey::Initialize(gMemMgr);
  262.         
  263.         gServerKey = new TMemPGPkey(); 
  264.         
  265.         gReloadServerKey = true;
  266.     }
  267.  
  268.     catch (TMacException & ex)
  269.     {
  270.         err = ex.GetExceptionErr();
  271.     }
  272.     
  273.     catch (TPGPException &ex)
  274.     {
  275.         err = ex.GetExceptionErr();
  276.      }
  277.  
  278.      return err;    
  279. }
  280.  
  281.  
  282. // ---------------------------------------------------------------------------
  283. pascal    void    UAMCleanUP(void)
  284. // ---------------------------------------------------------------------------
  285. // 
  286. {
  287.       DebugStr("\pUAM UAMCleanUP");
  288.     try
  289.     {
  290. //        if(gClientKey) delete gClientKey;
  291. //        if(gServerKey) delete gServerKey;
  292.      
  293.         TPGPkey::Finalize();
  294.  
  295.         FinalizeServerMemory(gMemMgr);    
  296.     }
  297.  
  298.     catch (TMacException & ex)
  299.     {
  300.      }
  301.     
  302.     catch (TPGPException &ex)
  303.     {
  304.       }
  305.     
  306.     __terminate();
  307.  
  308. }
  309.  
  310.  
  311.  
  312.